package rbac.entity;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.pandawork.core.entity.AbstractEntity;

/**
 * RBAC 用户组 实体类.<br>
 * 记录了一个用户组拥有哪些角色.
 * 
 * @author Huadi
 * @author Zoufengyang
 */
@Entity
@Table(name = "t_rbac_group")
@NamedQueries( {
		@NamedQuery(name = "rbacUserGroup.getAllRbacUserGroups", query = "from RbacUserGroup"),
		@NamedQuery(name = "rbacUserGroup.deleteRbacUserGroupById", query = "delete from RbacUserGroup where id = :id") })
public class RbacUserGroup extends AbstractEntity {

	private static final long serialVersionUID = 7072910711674205913L;

	// ---=== Fields ===--- //
	/** 用户组实体 ID. */
	@Id
	@Column(name = "id", nullable = false, length = 11)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	@Column(name = "name", nullable = false, length = 255)
	private String groupName;

	/** <code>UserGroup</code> 中包含的 <code>Role</code> 实体. */
	@ManyToMany(targetEntity = RbacRole.class, fetch = FetchType.LAZY)
	@JoinTable(name = "t_rbac_group_role", joinColumns = @JoinColumn(name = "group_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
	private Set<RbacRole> roles;

	/** 在此用户组 (<code>RbacUserGroup</code>) 中的用户 (<code>RbacUser</code>) 实体. */
	@ManyToMany(targetEntity = RbacUser.class, mappedBy = "userGroups", fetch = FetchType.LAZY)
	private Set<RbacUser> usersBelongsTo;

	// ---=== Getters and Setters ===--- //
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the groupName
	 */
	public String getGroupName() {
		return groupName;
	}

	/**
	 * @param groupName
	 *            the groupName to set
	 */
	public void setGroupName(String groupName) {
		this.groupName = groupName;
	}

	/**
	 * @return the roles
	 */
	public Set<RbacRole> getRoles() {
		return roles;
	}

	/**
	 * @param roles
	 *            the roles to set
	 */
	public void setRoles(Set<RbacRole> roles) {
		this.roles = roles;
	}

	/**
	 * @return the usersBelongsTo
	 */
	public Set<RbacUser> getUsersBelongsTo() {
		return usersBelongsTo;
	}

	/**
	 * @param usersBelongsTo
	 *            the usersBelongsTo to set
	 */
	public void setUsersBelongsTo(Set<RbacUser> usersBelongsTo) {
		this.usersBelongsTo = usersBelongsTo;
	}

}
